package com.example.bzh.gamecat.ui.live.newest;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.classic.adapter.CommonAdapter;
import com.example.bzh.gamecat.R;
import com.example.bzh.gamecat.bean.newest.DataBean;
import com.example.bzh.gamecat.bean.newest.DistractionListBean;
import com.example.bzh.gamecat.bean.newest.Newest;
import com.example.bzh.gamecat.config.HttpUrl;
import com.example.bzh.gamecat.httputils.newestutils.DataModel;
import com.example.bzh.gamecat.ui.live.newest.show.ShowActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class NewestFragment extends Fragment{

    private NewestContract.Model model;
    private NewestContract.Presenter presenter;
    @BindView(R.id.gv_newest_live)
    GridView gvNewestLive;
    private Unbinder unbinder;
    List<DistractionListBean> list;
    private NewestAdapter adapter;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==1){
                DataBean dataBean = (DataBean) msg.obj;
                list = dataBean.getDistractionList();
                Log.i("list","list======"+list.size());
                adapter = new NewestAdapter(getActivity(), R.layout.news_item_show,list);
                gvNewestLive.setAdapter(adapter);
                gvNewestLive.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent intent = new Intent(getActivity(), ShowActivity.class);
                        intent.putExtra("url",list.get(i).getPullUrl());
                        startActivity(intent);
                    }
                });
            }
        }
    };

    public NewestFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_newest, container, false);
        unbinder = ButterKnife.bind(this, view);
        String url = "http://app.show.youximao.tv/m/getDistractionLive.action?current=0&pageSize=20&token=01a30e837881ad7a17b8fce9def84956&ppinf=0%7C1470998583%7C0%7CdWlkOjkyNDMwMnxuaWNrbmFtZToxMzM4NDczfHZlcmlmaWVkOjB8cmVndGltZToxNDcwOTk4NTgzfHVzZXJuYW1lOjE4NjMyMTAzNjI3";
        DataModel.getHomeData(getActivity(),url,mHandler);
        list = new ArrayList<>();

        return view;
    }

//    public HashMap<String,String> getInitPath(){
//        HashMap<String, String> params = new HashMap<>();
//        params.put("current", "0");
//        params.put("pageSize", "20");
//        params.put("token", "01a30e837881ad7a17b8fce9def84956");
//        params.put("ppinf", "0%7C1470998583%7C0%7CdWlkOjkyNDMwMnxuaWNrbmFtZToxMzM4NDczfHZlcmlmaWVkOjB8cmVndGltZToxNDcwOTk4NTgzfHVzZXJuYW1lOjE4NjMyMTAzNjI3");
//
//        return  params;
//    }

   @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

}
